﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZetaPrints.ZetaClient.Data;

namespace ZetaPrints.ZetaClient.Presentation
{
    public partial class FormPreferences : Form
    {
        #region private attributes
        /// <summary>
        /// The configuration class to be changed
        /// </summary>
        private ZetaClientConfiguration _zetaClientConfiguration;
        #endregion


        public FormPreferences()
        {
            InitializeComponent();

            AcceptButton = buttonOK;
            CancelButton = buttonCancel;

            /// disable this area until we ask on the
            /// radio button for this.
            textBoxFileExt.Enabled = false;

            /// initialize our file configuration access
            _zetaClientConfiguration = new ZetaClientConfiguration();

            /// do we download order details?
            if (_zetaClientConfiguration.DownloadOrderDetails)
            {
                radioButtonDownloadOrderDetailsWhenTheOrderIsDownloaded.Checked = true;
            }
            else
            {
                radioButtonDownloadOrderDetailsWhenIClickOnTheOrderForTheFirstTime.Checked = true;
            }

            /// get the extensions on the screen
            textBoxFileExt.Text = _zetaClientConfiguration.DownloadFileExtensions;

            /// are we downloading the files?
            if (_zetaClientConfiguration.DownloadFiles)
            {
                /// if there is any text on text box it means this is to download
                /// with specific entries
                if (textBoxFileExt.Text.Trim(' ').Length == 0)
                {
                    radioButtonDownloadAllTheFilesWhenDetailsAreDownloaded.Checked = true;
                }
                else
                {
                    radioButtonDownloadOnlyFileExtensionsBellowWhenOrderDetailsAreDownloaded.Checked = true;
                }
            }
            else
            {
                radioButtonDoNotDownloadAnyFiles.Checked = true;
            }

            string downloadRootDirectory = _zetaClientConfiguration.DownloadRootDirectory;

            /// do we have an specific path or a partial path? if partial we need to make
            /// it specific
            if (!downloadRootDirectory.Contains(':') && !downloadRootDirectory.Contains(@"\\"))
            {
                downloadRootDirectory = ZetaClientConfiguration.CurrentApplicationLocation + @"\" + downloadRootDirectory;
            }

            textBoxDownloadFileLocation.Text = downloadRootDirectory;
        }

        #region event handler
        /// <summary>
        /// if the radio button for the adding of extensions pop up we 
        /// want to give some choices
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButtonDownloadOnlyFileExtensionsBellowWhenOrderDetailsAreDownloaded_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonDownloadOnlyFileExtensionsBellowWhenOrderDetailsAreDownloaded.Checked)
            {
                textBoxFileExt.Enabled = true;
            }
            else
            {
                textBoxFileExt.Enabled = false;
            }
        }
        #region buttons
        /// <summary>
        /// When we get out we save and leave
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonOK_Click(object sender, EventArgs e)
        {
            bool goodToGo = true;

            /// save the entry
            if (radioButtonDownloadOrderDetailsWhenTheOrderIsDownloaded.Checked)
            {
                 _zetaClientConfiguration.DownloadOrderDetails = true;
            }
            else if( radioButtonDownloadOrderDetailsWhenIClickOnTheOrderForTheFirstTime.Checked )
            {
                _zetaClientConfiguration.DownloadOrderDetails = false;
            }

            /// remove any invalid charater from the file names
            string textBoxFileExtText = textBoxFileExt.Text.Trim(' ').
                Replace(" ", "").
                Replace(".", "").
                Replace("*", "").
                Replace("|", "").
                Replace(@"\", "").
                Replace("/", "").
                Replace("\"", "").
                Replace("<", "").
                Replace(">", "").
                Replace("?", "").
                Replace(":", "");

            /// if we have double commas replace it with
            /// single comma, keep doing until there is none
            /// left
            while (textBoxFileExtText.Contains(",,"))
            {
                textBoxFileExtText = textBoxFileExtText.Replace(",,", ",");
            }

            /// remove the last comma of the string
            if (textBoxFileExtText.EndsWith(","))
            {
                textBoxFileExtText = textBoxFileExtText.Substring(0, textBoxFileExtText.Length - 1);
            }

            /// are we downloading the files?
            if (radioButtonDownloadAllTheFilesWhenDetailsAreDownloaded.Checked)
            {
                _zetaClientConfiguration.DownloadFiles = true;
                _zetaClientConfiguration.DownloadFileExtensions = string.Empty;
            }
            else if (radioButtonDownloadOnlyFileExtensionsBellowWhenOrderDetailsAreDownloaded.Checked)
            {
                if (textBoxFileExtText.Length > 0)
                {
                    _zetaClientConfiguration.DownloadFiles = true;
                    _zetaClientConfiguration.DownloadFileExtensions = textBoxFileExtText;
                }
                else
                {
                    /// if we selected to download file extensions bellow but selected no file
                    /// extension to be downloaded we simply do not download any files
                    _zetaClientConfiguration.DownloadFiles = false;
                    _zetaClientConfiguration.DownloadFileExtensions = string.Empty;
                }
            }
            else if (radioButtonDoNotDownloadAnyFiles.Checked)
            {
                _zetaClientConfiguration.DownloadFiles = false;
                _zetaClientConfiguration.DownloadFileExtensions = string.Empty;
            }

            if (textBoxDownloadFileLocation.Text != _zetaClientConfiguration.DownloadRootDirectory)
            {
                if (Directory.Exists(textBoxDownloadFileLocation.Text))
                {
                    string downloadFileLocation = textBoxDownloadFileLocation.Text.ToLower();

                    /// do we have the current directory as part of the path?
                    /// If yes we need to remove that piece
                    if (downloadFileLocation.Contains(ZetaClientConfiguration.CurrentApplicationLocation.ToLower()))
                    {
                        downloadFileLocation = downloadFileLocation.Remove(0, ZetaClientConfiguration.CurrentApplicationLocation.Length + 1);
                    }

                    _zetaClientConfiguration.DownloadRootDirectory = downloadFileLocation;
                }
                else
                {
                    MessageBox.Show(this, "Invalid directory selected, please try again", "Invalid directory selected", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    goodToGo = false;
                }
            }

            if (goodToGo)
            {
                _zetaClientConfiguration.Save();

                DialogResult = DialogResult.OK;
                Close();
            }
        }
        /// <summary>
        /// When the cancel button is pressed leave everything
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }
        /// <summary>
        /// We are going to browse for a directory
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonLocationBrowser_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            folderBrowserDialog.SelectedPath = textBoxDownloadFileLocation.Text;
            switch( folderBrowserDialog.ShowDialog(this) )
            {
                case DialogResult.OK:
                case DialogResult.Yes:
                    if (Directory.Exists(folderBrowserDialog.SelectedPath))
                    {
                        textBoxDownloadFileLocation.Text = folderBrowserDialog.SelectedPath;
                    }
                    else
                    {
                        MessageBox.Show(this, "Invalid directory selected, please try again", "Invalid directory selected", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    break;
            }
        }
        #endregion
        #endregion


    }
}
